/**
 * 
 */
package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.text.DecimalFormat;

/**
 * @author aviat
 *
 */
public class Statistics extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JLabel iconlabel;
	private JLabel lblStatistics;
	private JButton btnEventsINPeriod;
	private JButton btnEventsInHalls;
	private JButton btnMeetingsStatistics;
	private JButton btnUpcomingEvents;
	private JButton btnSignedNot;

	/**
	 * Constructor
	 */
	public Statistics() {
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(Statistics.class.getResource("/resources/back22.png")));
		btnEventsINPeriod = new JButton("Events In A Period");
		btnEventsINPeriod.setIcon(new ImageIcon(Statistics.class.getResource("/resources/Settings Clock.png")));
		btnEventsINPeriod.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnEventsInHalls = new JButton("Events In Halls");
		btnEventsInHalls.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnMeetingsStatistics = new JButton("Meetings Statistics");
		btnMeetingsStatistics.setIcon(new ImageIcon(Statistics.class.getResource("/resources/alliance.png")));
		btnMeetingsStatistics.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		
		initialize();
	}

	private void initialize() {
		lblStatistics = new JLabel("Statistics");
		lblStatistics.setForeground(new Color(0, 204, 255));
		lblStatistics.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblStatistics.setBounds(10, 11, 143, 25);
		add(lblStatistics);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		
		btnEventsINPeriod.setBounds(181, 149, 214, 67);
		add(btnEventsINPeriod);
		
		
		btnEventsInHalls.setBounds(434, 149, 214, 67);
		add(btnEventsInHalls);
		
		
		btnMeetingsStatistics.setBounds(63, 259, 214, 67);
		add(btnMeetingsStatistics);
		
		btnUpcomingEvents = new JButton("Upcoming Year Events");
		btnUpcomingEvents.setIcon(new ImageIcon(Statistics.class.getResource("/resources/calendar_year.png")));
		btnUpcomingEvents.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultCategoryDataset dataset = new DefaultCategoryDataset();
				dataset.setValue(100, "Events", "January");
				dataset.setValue(150, "Events", "February");
				dataset.setValue(45, "Events", "March");
				dataset.setValue(95, "Events", "April");
				JFreeChart chart = ChartFactory.createLineChart("Events In Period", "Month", "Events", dataset, PlotOrientation.VERTICAL, false, true, true);
				chart.getTitle().setPaint(Color.blue);
				final CategoryPlot p = chart.getCategoryPlot();
				BarRenderer renderer = (BarRenderer)p.getRenderer();
				DecimalFormat decimalformat = new DecimalFormat("##.##");
				renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalformat));
				p.setRenderer(renderer);
				renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));
				renderer.setItemLabelsVisible(true);
				chart.getCategoryPlot().setRenderer(renderer);
				
				p.setRangeGridlinePaint(Color.black);
				ChartFrame frame = new ChartFrame("Events In Period", chart);
				frame.setVisible(true);
				frame.setSize(450, 350);
				try {
					final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
					final File file1 = new File("UpcomingEvents.jpg");
					ChartUtilities.saveChartAsJPEG(file1, chart, 600, 400,info);
				} catch (Exception e2) {
					System.out.println("Couldn't save the Bar chart for Upcoming events");
				}
			}
		});
		btnUpcomingEvents.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnUpcomingEvents.setBounds(316, 259, 214, 67);
		add(btnUpcomingEvents);
		
		btnSignedNot = new JButton("Signed / Not Signed");
		btnSignedNot.setIcon(new ImageIcon(Statistics.class.getResource("/resources/Actions-document-sign-icon.png")));
		btnSignedNot.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnSignedNot.setBounds(562, 259, 214, 67);
		add(btnSignedNot);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @return the btnEventsINPeriod
	 */
	public JButton getBtnEventsINPeriod() {
		return btnEventsINPeriod;
	}

	/**
	 * @param btnEventsINPeriod the btnEventsINPeriod to set
	 */
	public void setBtnEventsINPeriod(JButton btnEventsINPeriod) {
		this.btnEventsINPeriod = btnEventsINPeriod;
	}

	/**
	 * @return the btnEventsInHalls
	 */
	public JButton getBtnEventsInHalls() {
		return btnEventsInHalls;
	}

	/**
	 * @param btnEventsInHalls the btnEventsInHalls to set
	 */
	public void setBtnEventsInHalls(JButton btnEventsInHalls) {
		this.btnEventsInHalls = btnEventsInHalls;
	}

	/**
	 * @return the btnMeetingsStatistics
	 */
	public JButton getBtnMeetingsStatistics() {
		return btnMeetingsStatistics;
	}

	/**
	 * @param btnMeetingsStatistics the btnMeetingsStatistics to set
	 */
	public void setBtnMeetingsStatistics(JButton btnMeetingsStatistics) {
		this.btnMeetingsStatistics = btnMeetingsStatistics;
	}

}